package com.lx.servletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建ServletContext
        ServletContext context = request.getServletContext();
        //获取传输的数据
        Object msg = context.getAttribute("msg");
        System.out.println(msg);
        //获取文件的服务器路径
        String a = context.getRealPath("/a.txt");          //web目录下资源访问
        String b = context.getRealPath("/WEB-INF/b.txt"); //WEB-INF目录下资源访问
        String c = context.getRealPath("/WEB-INF/classes/c.txt"); //src目录下资源访问
        File file = new File(a);

    }
}
